<template>
  <div class="shieldNum-box">
    <el-dialog
      :title="dialogTitle"
      width="650px"
      :visible.sync="dialogShow"
      :before-close="handleClose"
    >
      <el-form :model="form" :rules="rules" label-width="80px" ref="formRef">
        <el-form-item label="配置类型" prop="configType">
          <el-select
            v-model="form.configType"
            clearable
            filterable
            placeholder="请选择"
            style="width: 100%"
            disabled
          >
            <el-option
              v-for="(item, index) in configTypeOptionList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item 
         v-if="form.configType == 1"
         label="渠道" prop="productId">
          <el-select
            v-model="form.productId"
            clearable
            filterable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in upChannelOptionList"
              :key="index"
              :label="item.productName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
         v-if="form.configType == 2"
         label="产品" prop="productId">
          <el-select
            v-model="form.productId"
            clearable
            filterable
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in downProductOptionList"
              :key="index"
              :label="item.productName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="屏蔽号段" prop="shieldNum">
          <el-input
            style="width: 100%"
            v-model="form.shieldNum"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit('formRef')"
          >确 定</el-button
        >
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUpChannelList } from "@/api/business/channelManagement";
import { getDownProductList } from "@/api/business/productManagement";
import { shieldNumAdd } from "@/api/shieldNum/globalConfig";
export default {
  name: "shieldNumAdd",
  data() {
    return {
      // 弹框的显示状态
      dialogShow: false,
      // 弹框的标题
      dialogTitle: "",
      // 弹框的表单信息
      form: {},
      // 弹框表单校验规则
      rules: {},
      configTypeOptionList: [
        {
          label: "全局",
          value: 0,
        },
        {
          label: "渠道",
          value: 1,
        },
        {
          label: "产品",
          value: 2,
        },
      ],
      // 上游渠道筛选项
      upChannelOptionList: [],
      // 下游产品筛选项
      downProductOptionList: [],
    };
  },
  methods: {
    // 展示弹框
    showDilog(title){
      this.dialogShow = true;
      // 设置配置类型
      this.getOperateType(title)
      // 设置弹框标题
      this.dialogTitle = title
      if(this.dialogTitle != "添加全局配置"){
        // 获取去掉道列表
        this.getChannelList();
        // 获取产品列表
        this.getProductList();
      }

    },
    // 判断当前配置类型
    getOperateType(title) {
      switch (title) {
        case "添加全局配置":
          this.$set(this.form, "configType", 0);
          return 0;
        case "添加渠道配置":
          this.$set(this.form, "configType", 1);
          return 1;
        case "添加产品配置":
          this.$set(this.form, "configType", 2);
          return 2;
        default:
          this.$set(this.form, "configType", 3);
          return 3;
      }
    },
    // 获取渠道筛选项列表
    getChannelList() {
      // 获取上游渠道列表
      getUpChannelList().then((res) => {
        this.upChannelOptionList = res.data;
      });
    },
    // 获取产品筛选项列表
    getProductList() {
      // 获取下游产品列表
      getDownProductList().then((res) => {
        this.downProductOptionList = res.data;
      });
    },
    /*关闭弹窗*/
    handleClose() {
      this.$refs["formRef"].resetFields();
      this.form = this.$options.data().form;
      this.dialogShow = false;
    },
    /*提交操作*/
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {};
          params = JSON.parse(JSON.stringify(this.form));
          console.log(params);
          // 添加操作
          shieldNumAdd(params).then((res) => {
            if (res.code === 200) {
              this.$message.success("添加成功");
              this.handleRefresh();
            }
          });
        }
      });
    },
    // 刷新数据且关闭弹框
    handleRefresh() {
      // 通知父组件刷新数据
      this.$emit('refresh');
      // 关闭弹窗
      this.handleClose();
    },
  },
};
</script>

<style scoped lang='scss'>
</style>